package com.gy.bearhelps.setting.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gy.bearhelps.R;
import com.gy.bearhelps.setting.bean.DownFileBean;
import com.gy.bearhelps.util.ListViewItemListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：gy on 16/11/5
 * 说明：
 */
public class FileAdapter extends BaseAdapter {

    private Context context;
    private List<DownFileBean> mLists;
    private ListViewItemListener listener;
    private List<Integer> selects;
    private int isSelectALl;//1为全选
    private int isEdit;//是否为编辑状态

    public FileAdapter(Context context,List<DownFileBean> mLists){
        this.context = context;
        this.mLists = mLists;
        selects = new ArrayList<>();
    }

    /** 设置是否全选*/
    public void setIsSelectALl(int isSelectALl){
        this.isSelectALl = isSelectALl;
        if(isSelectALl == 0){
            selects.clear();
        }
        this.notifyDataSetChanged();
    }

    /** 设置选装条目*/
    public void setSelects(List<Integer> selects){
        this.selects = selects;
        notifyDataSetChanged();
    }

    /** 设置选中状态*/
    public void setIsEdit(int isEdit){
        this.isEdit = isEdit;
        if(isEdit == 0){
            selects.clear();
            notifyDataSetChanged();
        }
    }

    public void setListener(ListViewItemListener listener){
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return mLists.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if(view == null){
            holder = new ViewHolder();
            view = View.inflate(context, R.layout.adapter_file,null);
            holder.imgSelect = (ImageView) view.findViewById(R.id.adapter_file_img_select);
            holder.tvName = (TextView) view.findViewById(R.id.adapter_file_tv_title);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if(isEdit == 0){
            holder.imgSelect.setVisibility(View.GONE);
        } else {
            holder.imgSelect.setVisibility(View.VISIBLE);
            if(isSelectALl == 1){//如果全选
                holder.imgSelect.setImageResource(R.mipmap.ic_check_select);
            } else {
                if(selects.contains(position)){
                    holder.imgSelect.setImageResource(R.mipmap.ic_check_select);
                } else {
                    holder.imgSelect.setImageResource(R.mipmap.ic_check_normal);
                }
            }
        }
        holder.tvName.setText(mLists.get(position).name+"");

        return view;
    }

    class ViewHolder{
        ImageView imgSelect;
        TextView tvName;
    }
}
